using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace DiceRollerModel
{
    public class Button
    {
        public Rectangle Rect { get;  set; }
        public Texture2D PressedTexture { get;  set; }
        public Texture2D DepressedTexture { get; set; }
        internal bool pressed;
        public delegate void ButtonPushed();
        public event ButtonPushed ButtonPushedEventHandler;
        public void callButtonPushed()
        {
            ButtonPushedEventHandler.Invoke();
        }
        
        public bool HandleButtonPresses(TouchCollection touches)
        {
            Rectangle button = Rect;
            if (!pressed && touches.Count > 0)
            {
                foreach (TouchLocation loc in touches)
                {
                    if (loc.State == TouchLocationState.Pressed && button.Contains((int)loc.Position.X, (int)loc.Position.Y))
                    {
                        pressed = true;
                    }
                }
            }
            else if (pressed && touches.Count > 0)
            {
                foreach (TouchLocation loc in touches)
                {
                    if (loc.State == TouchLocationState.Released)
                    {
                        pressed = false;
                        if (button.Contains((int)loc.Position.X, (int)loc.Position.Y))
                        {
                            callButtonPushed();
                          
                        }
                    }
                }
            }
            return true;
        }

        public void DrawButton(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(pressed ? PressedTexture : DepressedTexture, Rect, Color.White);
        }
    }
}
